﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Activities;
using System.Activities.Runtime;
using System.DirectoryServices;

namespace Vision.Core.ActivityComponents.ActiveDirectory
{
    /// <summary>
    /// 
    /// </summary>
    public class GetUserByLogonName : CodeActivity
    {        
        /// <summary>
        /// 
        /// </summary>
        [DefaultValue(null)]
        public InArgument<string> LogonName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DefaultValue(null)]
        public OutArgument<ActiveDirectoryUserInfo> UserDetails { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(CodeActivityContext context)
        {
            if (!string.IsNullOrEmpty(context.GetValue(this.LogonName))){
                string filter = "(&(objectClass=Person)(sAMAccountName=" + context.GetValue(this.LogonName) + "))";
                DirectoryEntry directoryEntry = ActiveDirectoryHelper.GetDirectoryEntry(filter);
                context.SetValue(this.UserDetails, new ActiveDirectoryUserInfo(directoryEntry));
            }
        }
    }
}
